package com.yanfan.parking.quartz;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.ibms.common.utils.HttpUtil;
import com.yanfan.ibms.common.utils.HttpUtils2;
import com.yanfan.parking.domain.*;
import com.yanfan.parking.mapper.IbmsParkingPlaceMapper;
import com.yanfan.parking.service.INjCarWhiteInfoService;
import com.yanfan.parking.service.INjParkingCarInfoService;
import com.yanfan.parking.service.INjParkingDeviceInfoService;
import com.yanfan.parking.service.INjParkingHourFlowService;
import com.yanfan.parking.utils.OauthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 定时获取停车场车道信息
 */
@Component("parkingTask")
@Slf4j
public class ParkingTask {
    @Value("${api.dahua.url}")
    private String url;

    @Resource
    private IbmsParkingPlaceMapper ibmsParkingPlaceMapper;

    @Resource
    private INjParkingCarInfoService carInfoService;
    @Resource
    private INjParkingDeviceInfoService parkingDeviceInfoService;
    @Resource
    private INjCarWhiteInfoService carWhiteInfoService;
    @Resource
    private INjParkingHourFlowService hourFlowService;

    /**
     * 车辆管理
     * @throws Exception
     */
    public void getCarList() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                        url + "/evo-apigw/ipms/car/list"
                        ,headers,param);
        List<NjParkingCarInfo> carList = JSON.parseArray(JSONObject.parseObject(jsonObject.
                get("data").toString()).get("pageData").toString(), NjParkingCarInfo.class);
        carInfoService.saveOrUpdateBatch(carList);

    }

    /**
     * 停车场设备
     * @throws Exception
     */
    public void getParkingDevice() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                        url + "/evo-apigw/ipms/device/list"
                        ,headers,param);

        List<NjParkingDeviceInfo> deviceList = JSON.parseArray(JSONObject.parseObject(jsonObject.
                get("data").toString()).get("pageData").toString(), NjParkingDeviceInfo.class);
        parkingDeviceInfoService.saveOrUpdateBatch(deviceList);

    }

    /**
     * 黑白名单
     * @throws Exception
     */
    public void getcarWhiteList() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum","1");
        param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                        url + "/evo-apigw/ipms/carWhiteList/sendCars"
                        ,headers,param);
        List<NjCarWhiteInfo> list = JSON.parseArray(JSONObject.parseObject(jsonObject.
                        get("data").toString()).get("pageData").toString(),
                NjCarWhiteInfo.class);
       carWhiteInfoService.saveOrUpdateBatch(list);
    }

    /**
     * 获取停车场车道信息数据
     */
    public void getParkingLotInfo(){

        HashMap<String, Object> map = new HashMap<>();
        map.put("stationNo","stationNo");
        JSONObject jsonObject = HttpUtil.sendPost(url + "/" , null, map);
        List<IbmsParkingPlace> list = jsonObject.getList(jsonObject.toJSONString("parkingLotInfo"), IbmsParkingPlace.class);
        //批量更新或新增到数据库
        ibmsParkingPlaceMapper.insertOrUpdateBatch(list);
    }

    /**
     * 获取停车场车流量统计
     * @throws Exception
     */
    public void getParkCarFlow() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum","1");
        param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                        "https://192.168.254.6:443/evo-apigw/ipms/parkSurvey/carInOutSurvey?type=0"
                        ,headers,null);
        List<NjParkingHourFlow> parseArray = JSON.parseArray(JSONObject.parseObject(jsonObject.get("data").toString())
                .get("carInoutTrend").toString(), NjParkingHourFlow.class);
        String dateToStr = DateUtils.parseDateToStr("yyyyMMdd", new Date());
        ArrayList<NjParkingHourFlow> flows = new ArrayList<>();
        for (int i = 0; i < parseArray.size(); i++) {
            NjParkingHourFlow flow = parseArray.get(i);
            flow.setId(dateToStr + i);
            flow.setUpdateDate(dateToStr);
            flows.add(flow);
        }
        hourFlowService.saveOrUpdateBatch(flows);


    }

    /**
     * 车流量每小时统计
     */
   /* public void  flowHour() throws ParseException {

        Date nowDate = DateUtils.getNowDate();
        //Date time = DateUtils.dateTime("yyyy-MM-dd HH", DateUtils.parseDateToStr("yyyy-MM-dd HH", nowDate));
        log.info(DateUtils.parseDateToStr("yyyy-MM-dd HH", nowDate) + "车流量统计中...");

        // 获取当前时间往前推一小时的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        Date oneHourAgo = calendar.getTime();

        // 将日期格式化为字符串
        String nowDateStr = DateUtils.parseDateToStr("yyyy-MM-dd HH", nowDate);
        String oneHourAgoStr = DateUtils.parseDateToStr("yyyy-MM-dd HH", oneHourAgo);
        Date time = DateUtils.dateTime("yyyy-MM-dd HH", oneHourAgoStr);
        // 进场车流量数据
        List<IbmsParkingRecords> inList = ibmsParkingRecordsMapper.selectList(
                new LambdaQueryWrapper<IbmsParkingRecords>()
                        .eq(IbmsParkingRecords::getInOrOut, ParkingConstants.parkingIn)
                        .between(IbmsParkingRecords::getInTime,oneHourAgo, nowDate )
        );
        if (StringUtils.isNotEmpty(inList)){
            IbmsParkingFlowHour ibmsParkingFlowHour = new IbmsParkingFlowHour();
            ibmsParkingFlowHour.setInOut(Long.parseLong(ParkingConstants.parkingIn));//0代表进场
            ibmsParkingFlowHour.setInTime(time);
            ibmsParkingFlowHour.setInVehicleFlow(inList.size());
            ibmsFlowHourService.save(ibmsParkingFlowHour);
        }else {
            IbmsParkingFlowHour ibmsParkingFlowHour = new IbmsParkingFlowHour();
            ibmsParkingFlowHour.setInOut(Long.parseLong(ParkingConstants.parkingIn));//0代表进场
            ibmsParkingFlowHour.setInTime(time);
            ibmsParkingFlowHour.setInVehicleFlow(0);
            ibmsFlowHourService.save(ibmsParkingFlowHour);
        }
        //出场车流量数据
        List<IbmsParkingRecords> outList = ibmsParkingRecordsMapper.selectList(new LambdaQueryWrapper<IbmsParkingRecords>()
                .eq(IbmsParkingRecords::getInOrOut, ParkingConstants.parkingOut)
                .between(IbmsParkingRecords::getPassTime, oneHourAgo, nowDate));
        if (StringUtils.isNotEmpty(outList)){
            IbmsParkingFlowHour ibmsParkingFlowHour = new IbmsParkingFlowHour();
            ibmsParkingFlowHour.setInOut(Long.parseLong(ParkingConstants.parkingOut));//
            ibmsParkingFlowHour.setOutTime(time);
            ibmsParkingFlowHour.setOutVehicleFlow(outList.size());
            ibmsFlowHourService.save(ibmsParkingFlowHour);
        }else {
            IbmsParkingFlowHour ibmsParkingFlowHour = new IbmsParkingFlowHour();
            ibmsParkingFlowHour.setInOut(Long.parseLong(ParkingConstants.parkingOut));//1代表出场
            ibmsParkingFlowHour.setOutTime(time);
            ibmsParkingFlowHour.setOutVehicleFlow(0);
            ibmsFlowHourService.save(ibmsParkingFlowHour);
        }
    }*/
}
